package com.pshcp.doctor.api;

import com.framework.base.ResultBuilder;
import com.framework.base.ResultDTO;
import com.framework.exception.BizException;
import com.framework.logaop.Log;
import com.framework.logaop.LogSourceEnum;
import com.framework.logaop.LogTypeEnum;
import com.framework.thread.ReqComTool;
import com.framework.tool.CommonTool;
import com.pshcp.doctor.ao.patientcheckinsettings.SaveDoctorPatientCheckInSettingsAO;
import com.pshcp.doctor.tool.DoctorPatientCheckInSettingsTool;
import com.pshcp.doctor.vo.patientcheckinsettings.DoctorPatientCheckInSettingsVO;
import com.pshcp.domain.model.DoctorPatientCheckInSettings;
import com.pshcp.service.DoctorPatientCheckInSettingsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Description: TODO
 * @Author: xiaoche
 * @Date: 2022/11/3 18:14
 */
@Api(tags = {"医生端-患者报道设置接口"})
@Slf4j
@RestController
@RequestMapping("/api/doctor/patientCheckInSettings")
public class DoctorPatientCheckInSettingsAPI {

    @Autowired
    private DoctorPatientCheckInSettingsService doctorPatientCheckInSettingsService;

    @ApiOperation("获取医生患者报道设置")
    @Log(source = LogSourceEnum.APP, type = LogTypeEnum.SELECT, desc = "获取医生患者报道设置")
    @PostMapping("getDoctorPatientCheckInSettings")
    public ResultDTO<DoctorPatientCheckInSettingsVO> getDoctorPatientCheckInSettings() throws BizException {
        DoctorPatientCheckInSettings doctorPatientCheckInSettings = doctorPatientCheckInSettingsService.getDoctorPatientCheckInSettingsByDoctorId(ReqComTool.getUserId());
        if (doctorPatientCheckInSettings == null) {
            DoctorPatientCheckInSettings defaultDoctorPatientCheckInSettings = DoctorPatientCheckInSettingsTool.buildDefaultDoctorPatientCheckInSettings();
            doctorPatientCheckInSettingsService.insert(defaultDoctorPatientCheckInSettings);
            DoctorPatientCheckInSettingsVO defaultDoctorPatientCheckInSettingsVO = CommonTool.copyProperties(defaultDoctorPatientCheckInSettings, DoctorPatientCheckInSettingsVO.class);
            return ResultBuilder.success(defaultDoctorPatientCheckInSettingsVO);
        }
        DoctorPatientCheckInSettingsVO doctorPatientCheckInSettingsVO = CommonTool.copyProperties(doctorPatientCheckInSettings, DoctorPatientCheckInSettingsVO.class);
        return ResultBuilder.success(doctorPatientCheckInSettingsVO);
    }

    @ApiOperation("保存医生患者报道设置")
    @Log(source = LogSourceEnum.APP, type = LogTypeEnum.SELECT, desc = "保存医生患者报道设置")
    @PostMapping("saveDoctorPatientCheckInSettings")
    public ResultDTO<Long> saveDoctorPatientCheckInSettings(@RequestBody SaveDoctorPatientCheckInSettingsAO saveDoctorPatientCheckInSettingsAO) throws BizException {
        DoctorPatientCheckInSettingsTool.checkSaveDoctorPatientCheckInSettings(saveDoctorPatientCheckInSettingsAO);
        DoctorPatientCheckInSettings doctorPatientCheckInSettings = doctorPatientCheckInSettingsService.getDoctorPatientCheckInSettingsByDoctorId(ReqComTool.getUserId());
        if (doctorPatientCheckInSettings == null) {
            DoctorPatientCheckInSettings saveDoctorPatientCheckInSettings = DoctorPatientCheckInSettingsTool.buildSaveDoctorPatientCheckInSettings(saveDoctorPatientCheckInSettingsAO);
            doctorPatientCheckInSettingsService.insert(saveDoctorPatientCheckInSettings);
        } else {
            DoctorPatientCheckInSettings updateDoctorPatientCheckInSettings = DoctorPatientCheckInSettingsTool.buildSaveDoctorPatientCheckInSettings(doctorPatientCheckInSettings, saveDoctorPatientCheckInSettingsAO);
            doctorPatientCheckInSettingsService.update(updateDoctorPatientCheckInSettings);
        }
        return ResultBuilder.success(null);
    }
}
